@(log: uk.bl.monitrix.model.CrawlLog, stats: uk.bl.monitrix.model.CrawlStats, knownHosts: uk.bl.monitrix.model.KnownHostList, alerts: uk.bl.monitrix.model.AlertLog)

@import tags.{ navbar, alertTag, embedParam }

@main("Monitrix - Home") {
  @navbar("/")
}{
  <div class="container">
    <div class="row">
      <div style="padding:0px 0px 20px 40px;">
        <p style="padding:25px 0px 5px 0px;font-size:16px;color:#a2a2a2;">
              @if(log.isIdle) {
                Crawl Idle Since
              } else {
                Crawl Running Since
              }
        </p>
        <h1 id="wall-clock" style="margin:0px 0px 45px 0px;padding-top:0px;font-size:52px"></h1>
        <hr/>
      </div>
    </div>
  </div>

  <div class="container">
    <div class="row">
      <div class="span4" style="margin-top:5px;">
        <div class="monitrix-current-stats pull-left">
          <strong>Crawl Rate</strong>
          <div class="chart" id="gauge-crawl-rate" data-percent="0"><span>0</span></div>
          URLs/min
        </div>
        <div class="monitrix-current-stats pull-right">
          <strong>Download Rate</strong><br/>
          <div class="chart" id="gauge-download-rate" data-percent="0"><span>0</span></div>
          MB/min
        </div>
      </div>
         
      <div class="span7 offset1" style="margin-top:5px;">
        <div class="monitrix-current-stats pull-left">
          <strong>Total URLs</strong>
          @defining(uk.bl.monitrix.Global.getBackend.getCrawlLog) { log =>
            @defining(log.countRevisits) { revisited =>
              @defining(log.countEntries) { total =>
                <div class="chart" id="gauge-visited" data-percent="@{if (total > 0) { 100 * (total - revisited) / total} }"><span>@log.countEntries</span></div>
              }
            }
          }
          URLs
        </div>

        <p style="clear:both;margin-bottom:20px;"/>    
        
        <h5>Recent Alerts (out of @alerts.countAll alerts total) <a class="pull-right" style="font-weight:normal" href="@routes.Alerts.index@embedParam()">Details »</a></h5>
        <table class="table table-condensed">
          @for(alert <- alerts.getMostRecent(10)) {
            <tr class="monitrix-alert-table">
              <td>@alertTag(alert.getAlertType())</td>
              <td>@uk.bl.monitrix.NumberFormat.since(alert.getTimestamp, true)</td>
              <td><a href="@routes.Hosts.getHostInfo(alert.getOffendingHost)@embedParam()">
              @if( alert.getOffendingHost.length > 30 ) {
                @alert.getOffendingHost.substring(0,30)...
              } else {
                @alert.getOffendingHost
              }
              </a></td>
            </tr>
          }
        </table>
      </div>      
    </div>
  </div> 

  <link rel="stylesheet"type="text/css" href="@routes.Assets.at("stylesheets/jquery.easy-pie-chart.css")">
  <script type="text/javascript" src="@routes.Assets.at("javascripts/monitrix-clock.js")"></script>
  <script type="text/javascript" src="@routes.Assets.at("javascripts/jquery.easy-pie-chart.js")"></script>
  <script src="@routes.Assets.at("javascripts/raphael-min.js")" type="text/javascript"></script>
  <script src="@routes.Assets.at("javascripts/morris.min.js")" type="text/javascript"></script>
  <script type="text/javascript">
    var lastHoveredTLD;
    
    @if(log.isIdle) {
      new Monitrix.ProgressClock(@{System.currentTimeMillis - log.getTimeOfLastCrawlActivity}, $('#wall-clock'));
    } else {
      new Monitrix.ProgressClock(@{System.currentTimeMillis - log.getCrawlStartTime}, $('#wall-clock'));
    }
    
    function getAverage(logEntries) {
      var endTime = logEntries[0].timestamp;
      var startTime = logEntries[logEntries.length - 1].timestamp;
      var durationInMinutes = (endTime - startTime) / 60000; 
    		
      var crawl_rate, total_download_size = 0;
      if (durationInMinutes > 0) {
        crawl_rate = logEntries.length / durationInMinutes;

        for (var i=0; i<logEntries.length; i++) {
          total_download_size += logEntries[i].download_size;	 	
        }
      }
      
      return { crawl_rate: crawl_rate, download_rate: (total_download_size / durationInMinutes) };
    }
        
    @defining(log.getMostRecentEntries(1000)) { mostRecent =>
    function refreshGauges() {
      $.getJSON('/api/log/totals', function(data) {
          var visited = $('#gauge-visited');
          visited.data('easyPieChart').update( 100 * ((data[0]-data[1])/data[0]));
          $('span', visited).text(data[0]);
      });
      $.getJSON('/api/log/recent', function(data) {
    	//if ((Date.now() - data[0].timestamp) < 1200000) {
    	  var average = getAverage(data);
    	  
          var crawlRate = $('#gauge-crawl-rate');
		  var aveCrawlRate = @uk.bl.monitrix.analytics.LogAnalytics.getAverageCrawlRate(mostRecent.iterator);
          crawlRate.data('easyPieChart').update(50 * average.crawl_rate / aveCrawlRate);
          $('span', crawlRate).attr('title',aveCrawlRate);
          $('span', crawlRate).text(average.crawl_rate.toPrecision(5));
        	
          var downloadRate = $('#gauge-download-rate');
          var aveDownRate = @uk.bl.monitrix.analytics.LogAnalytics.getAverageDownloadRate(mostRecent.iterator);
          downloadRate.data('easyPieChart').update(50 * average.download_rate / aveDownRate);
          $('span', downloadRate).attr('title',(aveDownRate/(1024*1024)));
          $('span', downloadRate).text((average.download_rate / (1024 * 1024)).toPrecision(5) + 'MB');
    	//}

        window.setTimeout(refreshGauges, 1000);
      });
    }
    }
    	
    $(function() {
      $('.chart').easyPieChart({
        animate: 200,
        lineWidth: 7,
        lineCap: 'square',
        barColor: '#850303',
        size:120
      });
    });
     
    refreshGauges();
  </script>
}
